package ee.tlnk.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ee.tlnk.model.AccountModel;

/**
 * The Class AccountDAOImpl.
 */
@Repository
public class AccountDAOImpl implements AccountDAO {

	/** The session factory. */
	@Autowired
	private SessionFactory sessionFactory;

	/* (non-Javadoc)
	 * @see ee.tlnk.dao.AccountDAO#addAccount(ee.tlnk.model.AccountModel)
	 */
	public void addAccount(AccountModel account) {
		sessionFactory.getCurrentSession().save(account);
	}

	/* (non-Javadoc)
	 * @see ee.tlnk.dao.AccountDAO#listAccount()
	 */
	@SuppressWarnings("unchecked")
	public List<AccountModel> listAccount() {

		return sessionFactory.getCurrentSession().createQuery("from AccountModel")
			.list();
	}

	/* (non-Javadoc)
	 * @see ee.tlnk.dao.AccountDAO#removeAccount(java.lang.Integer)
	 */
	public void removeAccount(Integer id) {
		AccountModel account = (AccountModel) sessionFactory.getCurrentSession().load(
				AccountModel.class, id);
		if (null != account) {
			sessionFactory.getCurrentSession().delete(account);
		}

	}
}
